var ZYRequestClient = require("../../../../utils/zy-request");
var requestClient;
const app = getApp();
Page({
  data:{
    showCompanyAction:false,
    showUserDialog:false,
    radioItems: [],
    selectCompany:null,
    companyActionButtons:[],
    companyActionButtons_a:[{text:'查看详情',value:'info'},{text:'转让企业',value:'transfer'},{text:'变更企业名称',value:'change'},{text:'解散企业',type: 'warn',value:'dissolution'}],
    companyActionButtons_b:[{text:'查看详情',value:'info'},{text:'退出企业',type: 'warn',value:'quit'}],
    companyActionButtons_c:[{text:'查看详情',value:'info'},{text:'转让企业',value:'transfer'},{text:'解散企业',type: 'warn',value:'dissolution'}],
    navbarData:{
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '我的企业', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     height: app.globalData.height * 2 + 20
  },
  onLoad() {
    requestClient = new ZYRequestClient();
  },
  onShow(){
    this.loadMyCompanyList();
  },
  // 加载公司列表
  loadMyCompanyList(){
    var _this = this;
    var userInfo = wx.getStorageSync('userInfo');
    if(userInfo ==''){
      wx.showModal({
        title:'请授权登录！',
        content:'您还未授权登录！',
        showCancel:false,
        success(r){
          wx.reLaunch({
            url: '../../../login/index',
          })
        }
      })
    } else {
      requestClient.getMyCompanyList(userInfo.id,function(res){
        if(res.data.data.length > 0){
            var myCompanyList = res.data.data;
            var radioItems = [];
            for(var i=0;i<myCompanyList.length;i++){
                var company = myCompanyList[i];
                var myCompany = {
                  id:company.id,
                  name:company.name+"-" +company.belongUserName,
                  value:company.id,
                  belongUser:company.belongUser,
                  checked:company.defaultCompany,
                  authStatus:company.authStatus
                };
                radioItems.push(myCompany);
            }
            _this.setData({
              radioItems:radioItems
            });
        } else {
          wx.showToast({
            title: '请完善企业信息！',
            icon:'error',
            mask:true
          })
          setTimeout(() => {
            wx.switchTab({
              url: '../../contacts/index',
            })
          }, 1000);
        }
    });
    }
  },
  // 加载公司所有用户
  loadCompanyAllUserList(){
    var _this = this;
    requestClient.getAllUserList(_this.data.selectCompany.id,function(res){
        _this.setData({
          companyUserList:res.data.data
        });
    });
  },
  // 切换公司
  radioChange:function(e){
    var _this = this;
    var userInfo = wx.getStorageSync('userInfo');
    requestClient.switchCompany(userInfo.id,e.detail.value,function(res){
      if(res.data.data){
        _this.loadMyCompanyList();
      }
    });
  },
  // 长按显示操作按钮
  pressCompany(e){
    var company = e.currentTarget.dataset.company;
    var _this = this;
    _this.setData({
      showCompanyAction:true,
      selectCompany:company
    });
    var userInfo = wx.getStorageSync('userInfo');
    // 判断公司创建者是否为本人
    if(company.belongUser == userInfo.id){
      // if(company.authStatus){
      //   _this.setData({
      //     companyActionButtons:_this.data.companyActionButtons_c
      //   });
      // } else {
      //   _this.setData({
      //     companyActionButtons:_this.data.companyActionButtons_a
      //   });
      // }
      _this.setData({
        companyActionButtons:_this.data.companyActionButtons_a
      });
    } else {
      _this.setData({
        companyActionButtons:_this.data.companyActionButtons_b
      });
    }
  },
  // 公司操作
  bindCompanyActionTap:function(e){
    var _this = this;
    _this.setData({
      showCompanyAction:false
    });
    if(e.detail.value == "info"){
      var companyId = _this.data.selectCompany.id;
      wx.navigateTo({
        url: '../companydetail/index?companyId=' + companyId,
      })
    }
    // 公司转让
    else if(e.detail.value == "transfer"){
      _this.setData({
        showUserDialog:true
      });
      _this.loadCompanyAllUserList();
    } 
    // 退出公司
    else if(e.detail.value == "quit"){
      wx.showModal({
        title:'退出企业',
        content:'您确认要退出此企业吗？',
        success(e){
          if(e.confirm){
              var userInfo = wx.getStorageSync('userInfo');
              requestClient.quitCompany(_this.data.selectCompany.id,userInfo.id,function(res){
                _this.loadMyCompanyList();
              })
          }
        }
      })
    } 
    // 变更主体
    else if(e.detail.value == "change"){
      var companyId = _this.data.selectCompany.id;
      var companyName = _this.data.selectCompany.name;
      companyName = companyName.split('-')[0];
      wx.navigateTo({
        url: '../changecompany/index?companyId=' + companyId + '&companyName=' + companyName,
      })
    }
    // 解散企业
    else {
      wx.showModal({
        title:'解散企业',
        content:'解散企业后将删除该企业所有的用户和印章信息，请谨慎操作！',
        success(e){
          if(e.confirm){
              requestClient.removeCompany(_this.data.selectCompany.id,function(res){
                _this.loadMyCompanyList();
              })
          }
        }
      })
    }
  },
  // 关闭用户选择框
  bindClosedUser(e){
    this.setData({
      showUserDialog:false,
    });
  },
  // 确认转让公司
  bindUserCheck(e){
    var _this = this;
    var checkUserId = e.detail.value;
    _this.setData({
      showUserDialog:false
    });
    wx.showModal({
      title:'转让企业',
      content:'转让企业后您不在是此企业的超级管理员了，您确认要转让此企业吗？',
      success(e){
        if(e.confirm){
            requestClient.transferCompany(_this.data.selectCompany.id,checkUserId,function(res){
              if(res.data.data){
                wx.showModal({
                  title:'转让企业',
                  content:'您的企业已经转让，您将不在是企业超级管理员！',
                  showCancel:false,
                  success(r){
                    _this.loadMyCompanyList();
                  }
                })
              } else {
                wx.showModal({
                  title:'转让失败',
                  content:'此用户已经创建这个企业！',
                  showCancel:false,
                  success(r){

                  }
                })
              }
            })
        }
      }
    })
  }
})